import 'package:flutter/material.dart';
import 'Constants.dart' show AppColors;
import 'Constants.dart' show Contants;
import 'package:weixinclone/conversion/conversion.dart';
import 'package:weixinclone/NavigationBarView.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData.light().copyWith(
          primaryColor: Color(AppColors.AppBarColors),
          cardColor: Color(AppColors.AppBarColors)),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<NavigationView> _list;
  List<Widget> pageView;

  PageController pageController;
  int _currentIndex = 0;

  @override
  void initState() {
    super.initState();
    pageController = PageController(initialPage: _currentIndex, keepPage: true);
    _list = [
      NavigationView(
        title: "微信",
        color: Colors.blueGrey,
        icon: IconData(0xe608, fontFamily: Contants.IconFontFamily),
        activeIcon: IconData(0xe603, fontFamily: Contants.IconFontFamily),
      ),
      NavigationView(
        title: "通讯录",
        color: Colors.blueGrey,
        icon: IconData(0xe601, fontFamily: Contants.IconFontFamily),
        activeIcon: IconData(0xe656, fontFamily: Contants.IconFontFamily),
      ),
      NavigationView(
        title: "发现",
        color: Colors.blueGrey,
        icon: IconData(0xe600, fontFamily: Contants.IconFontFamily),
        activeIcon: IconData(0xe671, fontFamily: Contants.IconFontFamily),
      ),
      NavigationView(
        title: "我",
        color: Colors.blueGrey,
        icon: IconData(0xe6c0, fontFamily: Contants.IconFontFamily),
        activeIcon: IconData(0xe626, fontFamily: Contants.IconFontFamily),
      ),
    ];

    pageView = [
      Conversion(),
      Container(
        color: Colors.blue,
      ),
      Container(
        color: Colors.green,
      ),
      Container(
        color: Colors.orange,
      ),
    ];
  }

  @override
  Widget build(BuildContext context) {
    BottomNavigationBar bottomNavigationBar = BottomNavigationBar(
      currentIndex: _currentIndex,
      items: _list.map((NavigationView view) {
        return view.item;
      }).toList(),
      onTap: (int index) {
        _handleBottombarIndex(index);
      },
      fixedColor: Color(AppColors.TabIconActiveColor),
      type: BottomNavigationBarType.fixed,
    );
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text("微信"),
        elevation: 0,
        actions: <Widget>[
          IconButton(
              icon: Icon(Icons.search),
              onPressed: () {
                print("点击了搜索");
              }),
          Container(width: 16.0),
//          IconButton(
//            icon: Icon(Icons.add),
//            onPressed: () {
//              print("点击了添加");
//            },
//          ),

          PopupMenuButton(
              onSelected: (String selected) {
                print("点击了$selected");
              },
              itemBuilder: (BuildContext context) {
                return <PopupMenuItem<String>>[
                  PopupMenuItem(
                      child: _bulidPopupMenuItem(0xe69e, "发起群聊"),
                      value: "group_chat"),
                  PopupMenuItem(
                      child: _bulidPopupMenuItem(0xe638, "添加朋友"),
                      value: "add_friend"),
                  PopupMenuItem(
                      child: _bulidPopupMenuItem(0xe61b, "扫一扫"),
                      value: "qr_scan"),
                  PopupMenuItem(
                      child: _bulidPopupMenuItem(0xe62a, "收付款"),
                      value: "payment"),
                  PopupMenuItem(
                      child: _bulidPopupMenuItem(0xe63d, "帮助与反馈"),
                      value: "help")
                ];
              },
              icon: Icon(Icons.add)),
          Container(width: 16.0)
        ],
      ),
      body: PageView.builder(
        itemBuilder: (BuildContext context, int index) {
          return pageView[index];
        },
        controller: pageController,
        onPageChanged: (int index) {
          setState(() {
            _currentIndex = index;
          });
        },
        itemCount: pageView.length,
      ),
      bottomNavigationBar: bottomNavigationBar,
    );
  }

  _bulidPopupMenuItem(int icon, String title) {
    return Row(
      children: <Widget>[
        Icon(
          IconData(icon, fontFamily: Contants.IconFontFamily),
          color: Color(AppColors.AppBarPopupMenuItemColor),
        ),
        Container(
          width: 12,
        ),
        Text(title,
            style: TextStyle(
                fontSize: 14,
                color: Color(AppColors.AppBarPopupMenuItemColor))),
      ],
    );
  }

  void _handleBottombarIndex(index) {
    if (_currentIndex == index) return;
    setState(() {
      _currentIndex = index;
      pageController.animateToPage(_currentIndex,
          duration: Duration(milliseconds: 200), curve: Curves.easeInOut);
    });
  }
}
